<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2024/1/19
 * Time: 11:54
 */
namespace app\common\helper;

use app\common\enum\Adminuser_typeEnum;
use app\models\EtcSellerTiktokProduct;
use sookie\auth\models\Adminuser;
use yii\base\Exception;
use yii\helpers\Html as H;
class CommonHelper
{
    static function rowDiv($content,$option = ['class'=>'col-md-6']){
        $str = "";
        if(is_array($content)){
            foreach ($content as $item){
                $str .= H::tag("div",$item,$option);
            }
        }else{
            $str = H::tag("div",$content,$option);
        }
        return H::tag("div",$str ,['class' =>"row"]);
    }


    static function makeGroup($arr,$num = 2){
        if($num<=0){
            return [];
        }
        $group_arr = [];
        foreach ($arr as $k => $v){
            $group_i = intval($k/$num);
            $group_arr[$group_i][] = $v;
        }
        return $group_arr;
    }

    public static function getMarketPlacePic($url){
        $encode = base64_encode($url);
        return "http://test.vikoc.com/tiktokimg/{$encode}";
    }

    public static function float($num , $precision = 2){
        return floatval(round($num,$precision));
    }

    public static function toK($num){
        if($num > 1000){
            return intval($num/1000)."k";
        }
        if($num > 1000000){
            return intval($num/1000)."m";
        }
        return $num;
    }

    public static function getIntByDay($day1,$day2){
        $timestamp1 = strtotime($day1);
        $timestamp2 = strtotime($day2);
        $days = intval(abs($timestamp2 - $timestamp1) / 86400);
        return $days;
    }

    public static function isOffice(){
        $info = Adminuser::getUserInfo();
        return $info->type === Adminuser_typeEnum::OFFICE;
    }

    public static function isSeller(){
        $info = Adminuser::getUserInfo();
        return $info->type === Adminuser_typeEnum::SELLER;
    }

    public static function notOfficeThrow(){
        if(!self::isOffice()){
            throw new Exception("请登录矩阵团队账号操作");
        }
    }

    public static function notSellerThrow(){
        if(!self::isSeller()){
            throw new Exception("请登录商家账号操作");
        }
    }

    public static function getFirstErrorStr($errors)
    {
        if ($errors){
            foreach ($errors as $item){
                return $item[0];
            }
        }

        return "";
    }

    public static function getProductName($product){
        $arr_pic = \yii\helpers\Json::decode($product['img'],true);
        $pic = \yii\helpers\ArrayHelper::getValue($arr_pic,'0');
        $str = <<<STR
<div class="ant-card ant-card-bordered ant-card-hoverable" style="width: 150px;">
    <div class="ant-card-cover">
        <img alt="{$product['name']}" src="{$pic}">
    </div>
    <div class="ant-card-body">
        <div class="ant-card-meta">
            <div class="ant-card-meta-detail">
                <div class="ant-card-meta-title">{$product['name']}</div>
            </div>
        </div>
    </div>
</div>
STR;

        return $str;
    }

    public static function getProductNameById($id){
        $product = EtcSellerTiktokProduct::findOne($id);
        return self::getProductName($product);
    }
}